package controller

import (
	"gin/common"
	"gin/response"
	"gin/service"
	"gin/validate"
	"github.com/gin-gonic/gin"
)

func SentenceWordSelect(ctx *gin.Context) {
	var selectDictQuery validate.SelectSentenceWordQuery

	err := ctx.ShouldBindJSON(&selectDictQuery)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := common.GenerateSelectQueryParam(selectDictQuery.Param, "log")

	total, resData, queryErr := service.SelectSentenceWord(selectQueryParam)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": gin.H{"total": total, "rows": resData}}, "success")
	}
}

// 查询单条词库
func SentenceWordFind(ctx *gin.Context) {
	var selectDictQuery validate.SelectSentenceWordQuery

	err := ctx.ShouldBindJSON(&selectDictQuery)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := common.GenerateSelectQueryParam(selectDictQuery.Param, "log")

	resData, queryErr := service.FindSentenceWord(selectQueryParam)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// 插入日志
func SentenceWordCreate(ctx *gin.Context) {
	var addSentenceWord validate.AddSentenceWordWrapper

	err := ctx.ShouldBindJSON(&addSentenceWord)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	resData, createErr := service.CreateSentenceWord(addSentenceWord.Data)
	if createErr != nil {
		// 插入失败
		response.Fail(ctx, nil, createErr.Error())
		return
	}

	response.Success(ctx, gin.H{"data": resData}, "success")
}

func SentenceWordUpdate(ctx *gin.Context) {
	var updateSentenceWord validate.UpdateSentenceWordWrapper

	err := ctx.ShouldBindJSON(&updateSentenceWord)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	resData, createErr := service.UpdateSentenceWord(updateSentenceWord.Data)
	if createErr != nil {
		// 插入失败
		response.Fail(ctx, nil, createErr.Error())
		return
	} else {
		// 插入数据成功
		updateSentenceWord.Data.Id = resData.Id
		updateSentenceWord.Data.Created = resData.Created
	}

	response.Success(ctx, gin.H{"data": updateSentenceWord.Data}, "success")
}

func SentenceWordDelete(ctx *gin.Context) {
	var deleteSentenceWordParam validate.DeleteSentenceWordParam

	err := ctx.ShouldBindJSON(&deleteSentenceWordParam)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := common.GenerateFilterParam(&deleteSentenceWordParam.Param, "sentence_word")

	service.DeleteSentenceWord(&selectQueryParam)

	// 删除成功
	response.Success(ctx, nil, "success")

}

// 查询相似词
func SentenceWordSimilarWords(ctx *gin.Context) {
	var selectDictQuery validate.QuerySimilarWords

	err := ctx.ShouldBindJSON(&selectDictQuery)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := common.GenerateSelectQueryParam(selectDictQuery.Param, "log")

	res, queryErr := service.QuerySimilarWords(selectQueryParam, selectDictQuery.Number, selectDictQuery.IsAsync == "yes")

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": res}, "success")
	}
}
